﻿Public Class Frm_mawa3id
    Public avo As Rendez_vous
    Public Sub videz()
        txt_nom.Clear()
        txt_molahadat.Clear()
        txt_cause.Clear()
        dteTimPickHeur.Value = Now
        txt_nom.Select()
    End Sub
    Public Sub recherche_Red()
        List_tous.Items.Clear()
        For Each av As Rendez_vous In Partager.moi.List_rendez_vous
            If av.Date_rendez_vous >= Date_min.Value.Date And av.Date_rendez_vous <= Date_illa.Value.Date Then
                List_tous.Items.Add(New ListViewItem(New String() {av.Id, av.Nom_complet, av.Date_rendez_vous, av.Heure, av.Cause, av.Remarque}))
            End If
        Next
    End Sub
    Private Sub Frm_mawa3id_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_nom.Select()
        Date_illa.Value = Now.AddDays(1)
        Try
            Partager.moi.remplir_List_rendez_vous()
            ' List_tous.Items.Clear()
            recherche_Red()
            For Each khib As Rendez_vous In Partager.moi.List_rendez_vous
                'List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Nom_complet, khib.Date_rendez_vous, khib.Heure, khib.Cause, khib.Remarque}))
                If Not Partager.rendVsStruct.rendezVous Is Nothing Then
                    If khib.Id = Partager.rendVsStruct.rendezVous.Id And Partager.detail Then
                        List_tous.Items(List_tous.Items.Count - 1).Selected = True
                        List_tous.Items(List_tous.Items.Count - 1).Focused = True
                        Partager.detail = False
                    End If
                End If
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_nom.Select()
        recherche_Red()
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة موعد جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
               
                If (txt_nom.Text <> "") Then
                    If MsgBox("هل تريد فعلا إضافة هذا الموعد ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة المواعيد") = MsgBoxResult.Yes Then
                        Try
                            Partager.close()
                            Dim cli As New Rendez_vous(1, Date_maw3id.Value, txt_nom.Text, txt_cause.Text, dteTimPickHeur.Value.ToString("HH:mm"), txt_molahadat.Text)
                            Partager.moi.Ajouter_rendez_vous(cli)
                            recherche_Red()
                            MsgBox("تمت إضافة الموعد بنجاح", MsgBoxStyle.Information, "نجاح")
                            videz()
                        Catch ex As Exception

                            MsgBox(ex.Message)

                        End Try
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                Dim i As Integer = 0
                Dim id As Integer = List_tous.SelectedItems(0).Text

                Dim j As Integer = 0
                While i < List_tous.SelectedIndices.Count
                    avo = Partager.moi.List_rendez_vous(j)
                    If avo.Id = id Then
                        Date_maw3id.Value = avo.Date_rendez_vous
                        txt_nom.Text = avo.Nom_complet
                        txt_cause.Text = avo.Cause
                        dteTimPickHeur.Value = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, CInt(avo.Heure.Split(":")(0)), CInt(avo.Heure.Split(":")(1)), 0)
                        txt_molahadat.Text = avo.Remarque
                        i += 1
                    End If
                    j = j + 1
                End While
            End If
            If List_tous.SelectedIndices.Count <= 0 Then
                videz()
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الموعد ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف المواعيد") = MsgBoxResult.Yes Then
                Try
                    Dim clt As Rendez_vous = Partager.moi.List_rendez_vous(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_rendez_vous(avo.Id)
                    recherche_Red()
                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    videz()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر موعد من القائمة", MsgBoxStyle.Information, "بحث")
        End If

    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If (txt_nom.Text <> "") Then
                If MsgBox("هل تريد فعلا تعديل هذا الموعد ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل المواعيد") = MsgBoxResult.Yes Then
                    Try
                        Partager.close()
                        Dim cli As New Rendez_vous(1, Date_maw3id.Value, txt_nom.Text, txt_cause.Text, dteTimPickHeur.Value.ToString("HH:mm"), txt_molahadat.Text)
                        Partager.moi.modifier_rendez_vous(cli, avo.Id)
                        recherche_Red()
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        videz()
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try

                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر موعد من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If

    End Sub

    Private Sub Date_min_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Date_min.ValueChanged
        recherche_Red()
    End Sub

    Private Sub Date_illa_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Date_illa.ValueChanged
        recherche_Red()
    End Sub
End Class